<?php

namespace app\manage\controller;

use core\libs\db;
use core\libs\input;

class ads extends base {
    // 初始化
    public function __init() {
        $this->input = input::request(
            'id', 'action', 
            'search_key', 'search_cid', 'search_state', 
            'ad_pic', 'ad_name', 'ad_time', 'ad_position', 'ad_category', 'ad_content_id', 'ad_describe', 'ad_url', 'ad_enable', 'ad_type', 'ad_order', 'ad_video', 'ad_target'
        );

        // 是否有权限
        $this->is_level($this->baseClass . "_list");
        if(in_array($this->baseAction, ["submit", "delete", "batch"])) {
            $this->is_level($this->baseClass . "_edit");
        }
    }
    // 列表
    public function index() {
        extract($this->input);

        $filter = '';	
        if($search_cid != '') {
            $filter .= " and ad_position = '$search_cid'";
        }
        
        if($search_state != '') {
            $filter .= " and ad_enable = '$search_state'";
        }
        if($search_key) {
            $filter.=" and ad_name like '%$search_key%'";
        }

        $sql = "ad_position = position_id $filter";
        $page = new \core\libs\page((int)db::getCount("ads, ads_position", $sql), 10);
        $page->args = $this->getArgs("", 1);
        $result = db::fetchAll(
            "ads, ads_position", 
            "$sql order by ad_order desc, ad_position, ad_id desc" . $page->limit
        );

        $this->assign("search", [
            'cid' => $search_cid,
            'state' => $search_state,
            'key' => $search_key
        ]);
        $this->assign("global", [
            "page" => $page,
            "ad_position_list" => db::fetchAll("ads_position")
        ]);
        $this->assign("result", $result);
        $this->display("ads_list");
    }
    // 编辑
    public function edit() {
        extract($this->input);

        $this->assign("row", db::fetch("ads", "ad_id = '$id'"));
        $this->assign("global", [
            "id" => $id,
            "ad_position_list" => db::fetchAll("ads_position")
        ]);
        $this->display("ads_add");
    }
    // 添加、更新
    public function submit() {
        extract($this->input);

        $form_datas = array(
            'ad_position' => (int)$ad_position, 
			'ad_category' => (int)$ad_category, 
			'ad_content_id' => (int)$ad_content_id,
			'ad_name' => $ad_name,
			'ad_video' => $ad_video ? implode(',', $ad_video) : "",
			'ad_describe' => $ad_describe,
			'ad_pic' => $ad_pic ? implode(',', $ad_pic) : "",
			'ad_url' => $ad_url,
            'ad_target' => (int)$ad_target,
			'ad_enable' => (int)$ad_enable,
			'ad_type' => (int)$ad_type,
			'ad_order' => (int)$ad_order,
            'ad_time' => (int)strtotime($ad_time)
        );
        foreach($form_datas as $key => $val) {
            $form_datas[$key] = html_entity_decode($val);
        }

        if(empty($id)){
			//添加内容
			db::insert("ads", $form_datas);	
			alert('内容添加成功!', U($this->baseClass, $this->getArgs()));		
		}
		else{
			//更新	
			db::update("ads", $form_datas, "ad_id = '$id'");
			alert('内容更新成功!', U($this->baseClass, $this->getArgs()));
		}
    }
    // 删除
    public function delete() {
        extract($this->input);

        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        //删除内容
		db::delete('ads', "ad_id in ($id)");

        alert('删除成功!', U($this->baseClass, $this->getArgs()));		
    }
    // 批量操作
    public function batch() {
        extract($this->input);

        if(!$action) alert('请选择要操作的事件!', U($this->baseClass, $this->getArgs()));
        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        // 审核
        if($action == "state_yes" || $action == "state_no") {
            //更新	
            $state = array("state_yes" => 1, "state_no" =>0);
            db::update(
                "ads", array('ad_enable' => $state[$action]), "ad_id in ($id)"
            );
            alert('更新成功!', U($this->baseClass, $this->getArgs()));		
        }

        // 删除
        if($action == "delete") {
            $this->delete();
        }
    }
    // 获取固定地址栏参数
    protected function getArgs($args = "", $type = 0) {
        extract($this->input);
        $baseurl = "?search_cid={$search_cid}&search_state={$search_state}&search_key={$search_key}{$args}";
        return $this->get_base_args($baseurl, $type);
    }
}